package com.boarsoft.boar.server.action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.boarsoft.bean.ReplyInfo;
import com.boarsoft.boar.common.Constants;
import com.boarsoft.boar.entity.ServerInfo;
import com.boarsoft.boar.server.ServerBiz;
import com.boarsoft.common.Authorized;
import com.boarsoft.common.Util;
import com.boarsoft.common.dao.PagedResult;
import com.boarsoft.common.util.JsonUtil;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@RestController
@RequestMapping("/server")
public class ServerAction {
	@Autowired
	private ServerBiz serverBiz;

	@JsonIgnoreProperties(value = { "apps" })
	@RequestMapping("/list.do")
	@Authorized(code = "server.list")
	public ReplyInfo<Object> list(String projId, String env, String key, String orderBy, int pageNo, int pageSize) {
		PagedResult<ServerInfo> pr = serverBiz.list(projId, env, key, orderBy, pageNo, pageSize);
		return new ReplyInfo<Object>(true, pr);
	}

	// /**
	// * 按所属系统ID、应用ID查询应用未规划过服务器
	// *
	// http://localhost/deploy/server/listExclude.do?appId=9483859e5841dc1d015841eedf9b0002&projId=9483859e58761c610158767a39d50004&env=ALL&pageNo=1&pageSize=10
	// *
	// * @return
	// */
	// @RequestMapping("/listExclude.do")
	// @Authorized(code = "deploy.server.listExclude")
	// public ReplyInfo<String> list(String projId, String appId, String env,
	// String key, String orderBy, int pageNo, int pageSize) {
	// PagedResult<ServerInfo> pr = serverBiz.list(projId, appId, env, key,
	// orderBy, pageNo, pageSize);
	// return new ReplyInfo<String>(true, JsonUtil.from(pr, "apps"));
	// }
	//
	// /**
	// * 按库包ID分页查询所有关联的应用<br/>
	// * 以app_inst作为主表查询返回，返回appInst的同时冗余返回appinfo
	// */
	// @RequestMapping("/listApps")
	// @Authorized(code = "server.app.list")
	// public void listApps() {
	// PagedResult<AppInst> pr = null;
	// if (Util.strIsEmpty(serverId)) {
	// return new ReplyInfo<String>(Constants.ERROR_INVALID,
	// "Msg.common.param");
	// return;
	// }
	// pr = serverBiz.listAppsRich(serverId, key, orderBy, pageNo, pageSize);
	//
	// return new ReplyInfo<String>(true, JsonUtil.from(pr,
	// "server,insts,libs,svcs,files"));
	// }
	//
	// /**
	// * 按库包ID分页查询所有关联的应用<br/>
	// * 以app_info作为主表查询返回，只返回appinfo
	// */
	// @RequestMapping("/listApps1")
	// @Authorized(code = "server.listApps1")
	// public void listApps1() {
	// PagedResult<AppInfo> pr = null;
	// if (Util.strIsEmpty(serverId)) {
	// return new ReplyInfo<String>(Constants.ERROR_INVALID,
	// "Msg.common.param");
	// return;
	// }
	// pr = serverBiz.listApps(serverId, key, orderBy, pageNo, pageSize);
	//
	// return new ReplyInfo<String>(true, JsonUtil.from(pr,
	// "server,insts,libs,svcs,files"));
	// }
	//
	// /**
	// * 按库包ID分页查询所有未关联的应用<br/>
	// */
	// @RequestMapping("/listAppsNot")
	// @Authorized(code = "server.listAppsNot")
	// public void listAppsNot() {
	// if (Util.strIsEmpty(id)) {
	// return new ReplyInfo<String>(Constants.ERROR_INVALID,
	// "Msg.common.param");
	// return;
	// }
	// PagedResult<AppInfo> pr = serverBiz.listAppsWhereServerNotOnIt(id, key,
	// orderBy, pageNo, pageSize);
	//
	// return new ReplyInfo<String>(true, JsonUtil.from(pr,
	// "insts,libs,svcs,files"));
	// }

	/**
	 * 保存服务器
	 * 
	 * @return
	 */
	@RequestMapping("/save.do")
	@Authorized(code = "server.add")
	public ReplyInfo<String> save(ServerInfo a) {
		if (Util.strIsEmpty(a.getId())) {
			return this.add(a);
		}
		return this.update(a);
	}

	@RequestMapping("/add.do")
	@Authorized(code = "server.add")
	public ReplyInfo<String> add(ServerInfo a) {
		a.setStatus(ServerInfo.STATUS_RUNNING);
		serverBiz.save(a);
		return new ReplyInfo<String>(true, JsonUtil.from(a, "apps"));
	}

	@RequestMapping("/update.do")
	@Authorized(code = "server.update")
	public ReplyInfo<String> update(ServerInfo o) {
		ServerInfo a = serverBiz.get(o.getId());
		a.setEnv(o.getEnv());
		a.setGroup(o.getGroup());
		a.setCode(o.getCode());
		a.setName(o.getName());
		a.setIp(o.getIp());
		if (Util.strIsEmpty(a.getProjId())) {// 目前给projId赋默认值
			return new ReplyInfo<String>(Constants.ERROR_NOTFOUND, "Msg.common.project");
		}
		a.setType(o.getType());
		a.setStatus(ServerInfo.STATUS_RUNNING);
		a.setMemo(o.getMemo());
		serverBiz.update(a);
		return new ReplyInfo<String>(true, JsonUtil.from(a, "apps"));
	}

	@RequestMapping("/delete.do")
	@Authorized(code = "server.delete")
	public ReplyInfo<Object> delete(String id) {
		serverBiz.delete(id);
		return ReplyInfo.SUCCESS;
	}

	@JsonIgnoreProperties({ "apps" })
	@RequestMapping("/get.do")
	@Authorized(code = "server.get")
	public ReplyInfo<Object> get(String id) {
		ServerInfo o = serverBiz.get(id);
		return new ReplyInfo<Object>(true, o);
	}

	@RequestMapping("/enable.do")
	@Authorized(code = "server.enable")
	public ReplyInfo<Object> start(String id) {
		if (serverBiz.start(id)) {
			return ReplyInfo.SUCCESS;
		}
		return Constants.REPLY_WARN_INVALID;
	}

	@RequestMapping("/disable.do")
	@Authorized(code = "server.disable")
	public ReplyInfo<Object> stop(String id) {
		if (serverBiz.stop(id)) {
			return ReplyInfo.SUCCESS;
		}
		return Constants.REPLY_WARN_INVALID;
	}
}